package com.tgy.algorithm.base._概率问题;

public class _已知一个函数随机生成2到7的数 {


    public static int random1To7() {

        // [0,6)
//        double value = Math.random() * 6.0;
//        double val = Math.floor(value);
        return (int) (Math.floor(Math.random() * 7) + 1);
    }


    public static int random0Or1() {

        while (true) {
            // 生成 1到7之间到数，如果是4就重新生成，直到不是4为止
            int value = random1To7();
            if (value < 4) {
                return 0;
            }else if (value > 4){
                return 1;
            }
        }

    }

    public static int random20To30() {

        // [0,10] + 20;
        // 1111

        int value;
        while (true) {
            value = (random0Or1() << 3) + (random0Or1() << 2)
                    + (random0Or1() << 1) + (random0Or1() << 0);
            if (value <= 10) {
                break;
            }
        }

        return value+20;
    }

    public static void test01() {
        int count = 100000000;

        int equalCount = 0;
        for (int i = 0; i < count; i++) {
            // 1 2 3 4 5 6 7
            int val = random0Or1();
            if (val == 0) {
                equalCount++;
            }
        }

        System.out.println((equalCount * 1.0)/count);
    }

    public static void test02() {


        for (int j = 0; j < 100; j++) {
            int i = random20To30();
            System.out.println(i);
        }
    }

    public static void main(String[] args) {

//        test01();
        test02();
    }
}
